﻿function Scheduler() {
    var days = ["Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday" ];

    function Day(weekDay) {
        function Interval(startTime, endTime) {
            
            this.GetStartTime = function () {
                return startTime;
            }

            this.GetEndTime = function () {
                return endTime;
            }

        }

        var times = new Array();
        this.GetIntervals = function(){ return times; }
        this.GetWeekDay = function () { return weekDay; }
        this.GetWeekDayName = function () { return days[weekDay]; }
        function GetInterval(startTime, endTime) {
            var ix;
            for (ix = 0; ix < times.length; ++ix) {
                if (times[ix].GetStartTime() == startTime && times[ix].GetEndTime() == endTime) {
                    return times[ix];
                }
            }

            return null;
        };

        this.AddInterval = function (startTime, endTime) {
            var interval = GetInterval(startTime, endTime);

            if (interval == null) {
                interval = new Interval(startTime, endTime);
                times.push(interval);
            }

            return interval;
        };

        this.ContainsInterval = function (startTime, endTime) {
            var interval = GetInterval(startTime, endTime);

            if (interval == null) return false;

            return true;
        };

        this.RemoveInterval = function (startTime, endTime) {
            var interval = GetInterval(startTime, endTime);

            if (interval == null) {
                return;
            }

            for (var i = 0; i < times.length; ++i) {
                if (times[i].GetStartTime() == startTime && times[i].GetEndTime() == endTime) {
                    times = times.slice(i, i);
                    return interval;
                }
            }

            return;
        };
    };

    var schedule = new Array();

    function GetWeekday(weekDay) {
        var ix;
        for (ix = 0; ix < schedule.length; ++ix) {
            if (schedule[ix].GetWeekDay() == weekDay) {
                return schedule[ix];
            }
        }
        
    };

    this.AddInterval = function (weekDay, startTime, endTime) {
        var day = GetWeekday(weekDay);

        if (day == null) {
            day = new Day(weekDay);
            schedule.push(day);
        }

        day.AddInterval(startTime, endTime);

        return day;
    };

    this.ContainsInterval = function (weekDay, startTime, endTime) {
        var day = GetWeekday(weekDay);

        if (day == null) return false;

        return day.ContainsInterval(startTime, endTime);
    };

    this.RemoveInterval = function (weekDay, startTime, endTime) {
        var day = GetWeekday(weekDay);

        if (day == null) {
            return;
        }

        day.RemoveInterval(startTime, endTime);

        return day;
    };

    this.GetSchedule = function () { return schedule; }
};

function SchedulerView() {
    var Controller;

    this.SetController = function (controller) {
        Controller = controller;
    };

    this.ClickedInterval = function (day, startTime, endTime) {
        var tds = $('table tr > td:first-child:contains(' + startTime.toTimeString().substring(0, 8) + ' - ' + endTime.toTimeString().substring(0, 8) + ')').parent().children();
                
        $(tds[day + 1]).toggleClass("scheduler-selected");
    };

    function GetHour(timeString) {
        return timeString.substring(0, timeString.indexOf(":"));
    }

    function GetMinute(timeString) {
        return timeString.substring(timeString.indexOf(":") + 1, timeString.lastIndexOf(":"));
    }

    function GetSeconds(timeString) {
        return timeString.substring(timeString.lastIndexOf(":") + 1);
    }

    this.Initialize = function () {
        $('table td:not(:first-child)').click(function (event) {
            var intervalNode = event.currentTarget.previousElementSibling; var dayCounter = 0;
            while (intervalNode.previousElementSibling) {
                dayCounter++;
                intervalNode = intervalNode.previousElementSibling;
            }
            var indexOfSeparator = intervalNode.firstChild.data.indexOf(" - ");
            var startTime = intervalNode.firstChild.data.slice(0, indexOfSeparator);
            var endTime = intervalNode.firstChild.data.slice(indexOfSeparator + 3);

            var startTimeDate = new Date();
            startTimeDate.setHours(GetHour(startTime), GetMinute(startTime), GetSeconds(startTime));
            var endTimeDate = new Date();
            endTimeDate.setHours(GetHour(endTime), GetMinute(endTime), GetSeconds(endTime));

            Controller.SelectInterval(dayCounter, startTimeDate, endTimeDate);
        });
    };
};


function SchedulerController(view, model) {
    view.SetController(this);

    $(function() {
        view.Initialize();
    });

    this.SelectInterval = function (day, startTime, endTime) {
        if (model.ContainsInterval(day, startTime, endTime)) {
            model.RemoveInterval(day, startTime, endTime);
        } else {
            model.AddInterval(day, startTime, endTime);
        }
        view.ClickedInterval(day, startTime, endTime);
    };

    this.GetSchedule = function () { return model.GetSchedule(); }
};

